package com.canbot.u05.adapter.dance

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.canbot.u05.R
import com.canbot.u05.bean.dance.DanceMusicItemBean
import kotlinx.android.synthetic.main.dance_choose_music_item.view.*

class DanceMusicAdapter(internal val mContext: Context, private val mbrowList: MutableList<DanceMusicItemBean>) : RecyclerView.Adapter<DanceMusicAdapter.MyViewHolder>() {

        private var index: Int = 0
        inner class MyViewHolder(view: View,parent: ViewGroup) : RecyclerView.ViewHolder(view) {
                var iconImageView: ImageView = view.image
                var btn_sure = view.btn_sure
                var textView = view.text

        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                val view = LayoutInflater.from(mContext).inflate(R.layout.dance_choose_music_item, parent, false)
                return MyViewHolder(view,parent)
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                val (audio,isChecked,function) = mbrowList[position]

                holder.textView.text = audio.name.substring(0,audio.name.lastIndexOf("."))
                holder.btn_sure.isSelected = isChecked
                holder.iconImageView.setOnClickListener{
                        //设置选中
                        mbrowList.forEach {
                                it.isChecked = false
                                holder.btn_sure.isSelected = false
                        }
                        val dancePicItemBean = mbrowList.get(position)
                        if (holder.btn_sure.isSelected){
                                holder.btn_sure.isSelected = false
                                dancePicItemBean.isChecked = false

                        }else{
                                holder.btn_sure.isSelected = true
                                dancePicItemBean.isChecked = true
                        }
                        mbrowList[position] = dancePicItemBean
                        function(position,mbrowList.get(position).audio)
                }
        }

        override fun getItemId(i: Int): Long {
                return i.toLong()
        }

        override fun getItemCount(): Int {
                return mbrowList.size
        }

        fun setIndex(i: Int) {
                index = i
        }
}
